<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="风险申请记录id" prop="applyRecordId">
        <el-input v-model="formData.applyRecordId" placeholder="请输入风险申请记录id" />
      </el-form-item>
      <el-form-item label="风险辨识id" prop="identifyId">
        <el-input v-model="formData.identifyId" placeholder="请输入风险辨识id" />
      </el-form-item>
      <el-form-item label="风险辨识实例id" prop="identifyInstanceId">
        <el-input v-model="formData.identifyInstanceId" placeholder="请输入风险辨识实例id" />
      </el-form-item>
      <el-form-item label="风险对象id" prop="riskTargetId">
        <el-input v-model="formData.riskTargetId" placeholder="请输入风险对象id" />
      </el-form-item>
      <el-form-item label="风险对象" prop="riskTarget">
        <el-input v-model="formData.riskTarget" placeholder="请输入风险对象" />
      </el-form-item>
      <el-form-item label="风险对象内容id" prop="riskTargetContentId">
        <el-input v-model="formData.riskTargetContentId" placeholder="请输入风险对象内容id" />
      </el-form-item>
      <el-form-item label="风险对象内容" prop="riskTargetContent">
        <Editor v-model="formData.riskTargetContent" height="150px" />
      </el-form-item>
      <el-form-item label="风险程度" prop="riskDegree">
        <el-input v-model="formData.riskDegree" placeholder="请输入风险程度" />
      </el-form-item>
      <el-form-item label="风险等级" prop="riskLevel">
        <el-input v-model="formData.riskLevel" placeholder="请输入风险等级" />
      </el-form-item>
      <el-form-item label="风险等级" prop="fixedRiskLevel">
        <el-input v-model="formData.fixedRiskLevel" placeholder="请输入风险等级" />
      </el-form-item>
      <el-form-item label="真实风险等级" prop="realRiskLevel">
        <el-input v-model="formData.realRiskLevel" placeholder="请输入真实风险等级" />
      </el-form-item>
      <el-form-item label="风险对象内容所属单元id" prop="riskTargetContentUnitId">
        <el-input v-model="formData.riskTargetContentUnitId" placeholder="请输入风险对象内容所属单元id" />
      </el-form-item>
      <el-form-item label="风险对象内容所属单元名称" prop="riskTargetContentUnitName">
        <el-input v-model="formData.riskTargetContentUnitName" placeholder="请输入风险对象内容所属单元名称" />
      </el-form-item>
      <el-form-item label="风险对象内容负责人id" prop="riskTargetContentLeaderId">
        <el-input v-model="formData.riskTargetContentLeaderId" placeholder="请输入风险对象内容负责人id" />
      </el-form-item>
      <el-form-item label="检查周期" prop="checkCycle">
        <el-input v-model="formData.checkCycle" placeholder="请输入检查周期" />
      </el-form-item>
      <el-form-item label="检查周期单位" prop="checkCycleUnit">
        <el-input v-model="formData.checkCycleUnit" placeholder="请输入检查周期单位" />
      </el-form-item>
      <el-form-item label="检查内容" prop="checkContents">
        <el-input v-model="formData.checkContents" placeholder="请输入检查内容" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { RiskAssessmentRecordApi, RiskAssessmentRecordVO } from '@/api/zjyj/riskassessmentrecord'

/** 风险评估记录 表单 */
defineOptions({ name: 'RiskAssessmentRecordForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  applyRecordId: undefined,
  identifyId: undefined,
  identifyInstanceId: undefined,
  riskTargetId: undefined,
  riskTarget: undefined,
  riskTargetContentId: undefined,
  riskTargetContent: undefined,
  riskDegree: undefined,
  riskLevel: undefined,
  fixedRiskLevel: undefined,
  realRiskLevel: undefined,
  riskTargetContentUnitId: undefined,
  riskTargetContentUnitName: undefined,
  riskTargetContentLeaderId: undefined,
  checkCycle: undefined,
  checkCycleUnit: undefined,
  checkContents: undefined
})
const formRules = reactive({
  applyRecordId: [{ required: true, message: '风险申请记录id不能为空', trigger: 'blur' }],
  identifyId: [{ required: true, message: '风险辨识id不能为空', trigger: 'blur' }],
  identifyInstanceId: [{ required: true, message: '风险辨识实例id不能为空', trigger: 'blur' }],
  riskTargetId: [{ required: true, message: '风险对象id不能为空', trigger: 'blur' }],
  riskTarget: [{ required: true, message: '风险对象不能为空', trigger: 'blur' }],
  riskTargetContentId: [{ required: true, message: '风险对象内容id不能为空', trigger: 'blur' }],
  riskTargetContent: [{ required: true, message: '风险对象内容不能为空', trigger: 'blur' }],
  riskDegree: [{ required: true, message: '风险程度不能为空', trigger: 'blur' }],
  riskLevel: [{ required: true, message: '风险等级不能为空', trigger: 'blur' }],
  fixedRiskLevel: [{ required: true, message: '风险等级不能为空', trigger: 'blur' }],
  realRiskLevel: [{ required: true, message: '真实风险等级不能为空', trigger: 'blur' }],
  riskTargetContentUnitId: [{ required: true, message: '风险对象内容所属单元id不能为空', trigger: 'blur' }],
  riskTargetContentUnitName: [{ required: true, message: '风险对象内容所属单元名称不能为空', trigger: 'blur' }],
  riskTargetContentLeaderId: [{ required: true, message: '风险对象内容负责人id不能为空', trigger: 'blur' }],
  checkCycle: [{ required: true, message: '检查周期不能为空', trigger: 'blur' }],
  checkCycleUnit: [{ required: true, message: '检查周期单位不能为空', trigger: 'blur' }],
  checkContents: [{ required: true, message: '检查内容不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await RiskAssessmentRecordApi.getRiskAssessmentRecord(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as RiskAssessmentRecordVO
    if (formType.value === 'create') {
      await RiskAssessmentRecordApi.createRiskAssessmentRecord(data)
      message.success(t('common.createSuccess'))
    } else {
      await RiskAssessmentRecordApi.updateRiskAssessmentRecord(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    applyRecordId: undefined,
    identifyId: undefined,
    identifyInstanceId: undefined,
    riskTargetId: undefined,
    riskTarget: undefined,
    riskTargetContentId: undefined,
    riskTargetContent: undefined,
    riskDegree: undefined,
    riskLevel: undefined,
    fixedRiskLevel: undefined,
    realRiskLevel: undefined,
    riskTargetContentUnitId: undefined,
    riskTargetContentUnitName: undefined,
    riskTargetContentLeaderId: undefined,
    checkCycle: undefined,
    checkCycleUnit: undefined,
    checkContents: undefined
  }
  formRef.value?.resetFields()
}
</script>